package com.xup.luckymoney;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.SwitchPreference;

/**
 * Created by xupeng on 5/23/16.
 */
public class MainFragment extends PreferenceFragment implements
        Preference.OnPreferenceChangeListener {

    Preference listenImmediately;
    SwitchPreference weixinPref, qqPref, playNotice;
    private PreferenceCategory noticeCategory;
    private Config mConfig = null;
    private MediaPlayer player;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName(Config.PREFERENCE_NAME);
        addPreferencesFromResource(R.xml.main);

        mConfig = Config.getInstance(getActivity());
        initPreferences();
    }

    private void initPreferences() {
        // weixin switch
        weixinPref = (SwitchPreference) findPreference(Config.KEY_ENABLE_WECHAT);
        weixinPref.setOnPreferenceChangeListener(this);

        // QQ switch
        qqPref = (SwitchPreference) findPreference(Config.KEY_ENABLE_QQ);
        qqPref.setOnPreferenceChangeListener(this);

        // notice category
        noticeCategory = (PreferenceCategory) findPreference(Config.KEY_NOTICE_CATEGORY);

        // play notice when red packet come
        playNotice = (SwitchPreference) findPreference(Config.KEY_ENABLE_NOTICE);
        playNotice.setOnPreferenceChangeListener(this);

        // listen the notice
        listenImmediately = findPreference(Config.KEY_LISTEN_NOTICE);
        listenImmediately.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                MediaPlayer mediaPlayer = getPlayer();
                if (!mediaPlayer.isPlaying())
                    mediaPlayer.start();
                return true;
            }
        });

        updatePreferenceState();

        // what to after open hongbao packet
        final ListPreference wxAfterOpenPre = (ListPreference) findPreference(
                Config.EVNET_WECHAT_AFTER_OPEN_PACKET);
        wxAfterOpenPre
                .setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        int value = Integer.parseInt(String.valueOf(newValue));
                        preference.setSummary(wxAfterOpenPre.getEntries()[value]);
                        return true;
                    }
                });
        int value = Integer.parseInt(wxAfterOpenPre.getValue());
        wxAfterOpenPre.setSummary(wxAfterOpenPre.getEntries()[value]);

        final String haveDelay = getResources().getString(R.string.have_delayed);
        final String timeUnit = getResources().getString(R.string.time_unit_mm);
        final EditTextPreference delayEditTextPre = (EditTextPreference) findPreference(
                Config.KEY_RED_PACKET_DELAY_TIME);
        delayEditTextPre
                .setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        if ("0".equals(String.valueOf(newValue))) {
                            preference.setSummary("");
                        } else {
                            preference.setSummary(haveDelay + newValue + timeUnit);
                        }
                        return true;
                    }
                });
        String delay = delayEditTextPre.getText();
        if ("0".equals(String.valueOf(delay))) {
            delayEditTextPre.setSummary("");
        } else {
            delayEditTextPre.setSummary(haveDelay + delay + timeUnit);
        }
    }

    protected void updatePreferenceState() {
        Boolean isRunning = LuckyMoneyService.isRunning();
        weixinPref.setEnabled(isRunning);
        weixinPref.setDefaultValue(mConfig.getWehcatEnable());
        qqPref.setEnabled(isRunning);
        qqPref.setDefaultValue(mConfig.getQQEnable());
        playNotice.setEnabled(isRunning);
        playNotice.setChecked(mConfig.getNoticeEnable());
        if (playNotice.isChecked()) {
            noticeCategory.addPreference(listenImmediately);
        } else {
            noticeCategory.removePreference(listenImmediately);
        }
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        final String key = preference.getKey();
        boolean enabled = (Boolean) newValue;
        switch (key) {
            case Config.KEY_ENABLE_WECHAT:
                if ((Boolean) newValue && !LuckyMoneyService.isRunning()) {
                    ((SettingActivity) getActivity()).showOpenAccessibilityServiceDialog();
                }
                mConfig.setWechatEnable(enabled);
                break;
            case Config.KEY_ENABLE_QQ:
                if ((Boolean) newValue && !LuckyMoneyService.isRunning()) {
                    ((SettingActivity) getActivity()).showOpenAccessibilityServiceDialog();
                }
                mConfig.setQQEnable(enabled);
                break;
            case Config.KEY_ENABLE_NOTICE:
                mConfig.setNoticeEnable(enabled);
                break;
        }
        updatePreferenceState();

        return true;
    }

    public MediaPlayer getPlayer() {
        if (player == null)
            player = MediaPlayer
                    .create(getActivity(), R.raw.hongbao_arrived);

        return player;
    }
}